import ajax from "./ajax.js"
let h1 = document.querySelector(".sp h1");
let tbody = document.querySelector('table .goodsData');
let log = document.querySelector('.logon')
ajax.get('http://localhost:8000/getName', (value) => {
    // console.log(value);
    if(value){
        h1.innerHTML = `欢迎${value}来到主页`;
        log.style.display = 'none';
    }else{
        h1.innerHTML=`你没登录请先登录`;
        log.style.display = 'block';

    }
    

});
function del() {
    tbody.onclick = (event) => {

        if (event.target.innerHTML === "删除") {
            if (confirm("确定删除么")) {
                ajax.get("http://localhost:8000/delete", (value) => {
                    let data = JSON.parse(value);
                    if (value.acknowledged) {
                        window.open('http://localhost:8000/hPage', '_self');
                    }
                }, { id: event.target.id })
            }
        }

    }
}
del();


let btn = document.querySelector('.data button');
let goodsData = document.querySelector('.datas .goodsData');

btn.onclick = function () {
    ajax.get('http://localhost:8000/getGoods',(value)=>{
        let good = JSON.parse(value);
        goodsData.innerHTML="";
        good.forEach(function (item, index) {
            goodsData.innerHTML +=`
            <tr>
            <td>${item.goodsName}</td>
            <td>${item.goodsDetails}</td>
            <td>￥${item.price}</td>
            <td>${item.number}</td>
            <td><a href = "/goodsEdit?id=${item._id}">编辑</a>/<a href="javascript:;" id=${item._id}>删除</a></td>
            </tr>
            `;
        })
    })
};
btn.click();
